# delimit ;

clear all;
clear matrix;
set mem 800m;
set more off;

*add your path here:;
cd "C:\Users\jamesCabral\OneDrive - DAZ\CGH_Empirical_Replication";

*generate an empty matrix to store the results;
matrix T2 = J(2,5,.);

************************************************************************************;
import excel "data\ATUS_AHTUS_state_datasets\state_dataset_twoyear.xls", firstrow clear;

*merge with population weights;
merge m:1 idn using "data\uncertainty_popweights\state_pop_shares.dta";

gen popweight = popweight_2010;

************************************************************************************;
* Table 1, Column 1, Betas estimated using annual data;

xtset idn twoyear, yearly;

*Row 1;
regress D.homepaper D.workpaper D.male D.black D.married D.hvchild D.age1 D.age2 D.age3 D.age4 D.age5 D.educ1 D.educ2 D.educ3 D.educ4           [aw=popweight], vce(cluster idn);
gen beta_homepaper = _b[D.workpaper];
matrix T2[1,1] = _b[D.workpaper]*-1;

*Row 2;
regress D.leisurepaper D.workpaper D.male D.black D.married D.hvchild D.age1 D.age2 D.age3 D.age4 D.age5 D.educ1 D.educ2 D.educ3 D.educ4        [aw=popweight], vce(cluster idn);
gen beta_leisurepaper = _b[D.workpaper];
matrix T2[2,1] = _b[D.workpaper]*-1;

************************************************************************************;
* Table 1, Column 5, Elasticity;

*compute average time spent on three activities;
sum workpaper;
gen avg_workpaper = r(mean);

sum homepaper;
gen avg_homepaper = r(mean);

sum leisurepaper;
gen avg_leisurepaper = r(mean);

matrix T2[1,5] = beta_homepaper[1]*avg_workpaper[1]/avg_homepaper[1]*-1;
matrix T2[2,5] = beta_leisurepaper[1]*avg_workpaper[1]/avg_leisurepaper[1]*-1;

clear;
************************************************************************************;
* Table 1, Columns 2-4, Betas estimated using quarterly data in times of low- vs high-uncertainty;

import excel "data\ATUS_AHTUS_state_datasets\state_dataset_quarterly.xls", firstrow clear;

*merge with the population weights and uncertainty metric;
merge 1:1 year quarter idn using "data\uncertainty_popweights\state_uncertainty.dta";
merge m:1 idn using "data\uncertainty_popweights\state_pop_shares.dta", nogen;

*make an adjustment for quarters where we had zero observations;
*in this case, we assign values of zero to the missing variables;
foreach var in workpaper homepaper leisurepaper male black married hvchild age1 age2 age3 age4 age5 educ1 educ2 educ3 educ4 {;
	replace `var' = 0 if missing(`var') & _merge == 2;
};

gen date = mdy(3*quarter, 1, year);
gen qdate = qofd(date);
format qdate %tq;
xtset idn qdate, quarterly;
sort idn qdate;

*clear ;

gen popweight = popweight_2010;

* Generate year-on-year difference for time use of different activities;
gen YYworkpaper      		= (workpaper[_n]  - workpaper[_n-4]);
gen YYhomepaper      	  	= (homepaper[_n]  - homepaper[_n-4]);
gen YYleisurepaper 			= (leisurepaper[_n]  - leisurepaper[_n-4]);

* Generate year-on-year difference for control variables;
gen YYmale 					= (male[_n]  - male[_n-4]); 
gen YYblack 				= (black[_n]  - black[_n-4]);
gen YYmarried 				= (married[_n]  - married[_n-4]);
gen YYhvchild 			 	= (hvchild[_n]  - hvchild[_n-4]);
gen YYage1  				= (age1[_n]  - age1[_n-4]);
gen YYage2 					= (age2[_n]  - age2[_n-4]); 
gen YYage3 					= (age3[_n]  - age3[_n-4]); 
gen YYage4 					= (age4[_n]  - age4[_n-4]); 
gen YYage5 					= (age5[_n]  - age5[_n-4]);
gen YYeduc1 				= (educ1[_n]  - educ1[_n-4]); 
gen YYeduc2 				= (educ2[_n]  - educ2[_n-4]); 
gen YYeduc3 				= (educ3[_n]  - educ3[_n-4]); 
gen YYeduc4 				= (educ4[_n]  - educ4[_n-4]);

* Generate multiplication of dummy variable for high uncertainty quarters with the year-on-year difference for market hours worked;
gen YYworkpaperHUnct = YYworkpaper*highunctdummy_state;
gen YYworkpaperLUnct = YYworkpaper*(1-highunctdummy_state);

drop if year == 2003;

****************************************************************************************************************************************************;
* Nonmarket work;
* specification 1;
regress YYhomepaper YYworkpaperHUnct YYmale YYblack YYmarried YYhvchild YYage1 YYage2 YYage3 YYage4 YYage5 YYeduc1 YYeduc2 YYeduc3 YYeduc4           [aw=popweight];
est store four;
matrix T2[1,3] = _b[YYworkpaperHUnct]*-1;

* specification 2;
regress YYhomepaper YYworkpaperLUnct YYmale YYblack YYmarried YYhvchild YYage1 YYage2 YYage3 YYage4 YYage5 YYeduc1 YYeduc2 YYeduc3 YYeduc4           [aw=popweight];
est store three;
matrix T2[1,2] = _b[YYworkpaperLUnct]*-1;

* p-value testing that the two coefficients are the same;
* the null hypothesis that the estimated coefficient is the same in high and low-uncertainty times;
suest four three, vce(cluster idn);
test [four_mean]YYworkpaperHUnct = [three_mean]YYworkpaperLUnct;
matrix T2[1,4] = r(p);

drop _est_four;
drop _est_three;  

****************************************************************************************************************************************************;
* Leisure;
* specification 1;
regress YYleisurepaper YYworkpaperHUnct YYmale YYblack YYmarried YYhvchild YYage1 YYage2 YYage3 YYage4 YYage5 YYeduc1 YYeduc2 YYeduc3 YYeduc4        [aw=popweight];
gen HighUnct_9 = _b[YYworkpaperHUnct]*100;
est store four;
matrix T2[2,3] = _b[YYworkpaperHUnct]*-1;

* specification 2;
regress YYleisurepaper YYworkpaperLUnct YYmale YYblack YYmarried YYhvchild YYage1 YYage2 YYage3 YYage4 YYage5 YYeduc1 YYeduc2 YYeduc3 YYeduc4        [aw=popweight];
est store three;
matrix T2[2,2] = _b[YYworkpaperLUnct]*-1;

* p-value testing that the two coefficients are the same;
* the null hypothesis that the estimated coefficient is the same in high and low-uncertainty times;
suest four three, vce(cluster idn);
test [four_mean]YYworkpaperHUnct = [three_mean]YYworkpaperLUnct;
matrix T2[2,4] = r(p);

****************************************************************************************************************************************************;

*Compute the Elasticity;
matrix colnames T2 = "Beta_j" "Beta_L" "Beta_H" "P-value" "Elasticity";

*export results in a .csv file;
putexcel set "output\Table1_delimited.csv", replace;
putexcel B1 = matrix(T2), colnames;
putexcel A1 = "Time Use Categories";
putexcel A2 = "Nonmarket Work";
putexcel A3 = "Leisure";
